<?php

require_once(WSB_XTENSION_PATH . 'imagetext/models/DbTable/Table.php');

class ImageText_IndexController extends WSB_Xtension_Content
{
	private $_width;
	private $_height;

	public function init()
	{
		$config = $this->getConfiguration();
		$this->_width = isset($config->_width) ? $config->_width : 200 ;
		$this->_height = isset($config->_height) ? $config->_height : 200 ;
		parent::init();
	}

	public function indexAction()
	{
		$table = $this->getTable();
		$select = $table->select();
		$select->where('id=?', $this->_container->item_id);
		$row = $table->fetchRow($select);
		if($row instanceof WSB_Db_Row) {
			$image = new WSB_Image(WSB_PATH . trim($row->image));
			if($image->isFile()) {
				$width = empty($row->width) ? $this->_width : $row->width;
				$height = empty($row->height) ? $this->_height : $row->height;
				$scaledImage = $image->getScaledImage($width, $height);
				$this->assignToView('caption', trim($row->caption));
				$this->assignToView('alttext', trim($row->alttext));
				$this->assignToView('longtext', trim($row->longtext));
				$this->assignToView('width', $scaledImage->getWidth());
				$this->assignToView('height', $scaledImage->getHeight());
				$this->assignToView('image', $scaledImage);
				$this->renderView('index.phtml');
			}
		}
	}

	public function getTable()
	{
		return new ImageText_Model_DbTable_Table();
	}

	public function renderForBackend()
	{
		return $this->indexAction();
	}

}
